﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BskLeague.Models;

namespace BskLeague.Controllers
{
    public class TableController : Controller
    {

        BskLeagueDB db = new BskLeagueDB();

        public ActionResult Index()
        {
            var s_query = (from m in db.Seasons
                          where m.StatusId == 2
                          select m).ToList();

            List<TableTeamsHelper> TeamsList = new List<TableTeamsHelper>();
            ViewBag.IsTeams = false;

            if (s_query.Count() > 0)
            {
                var season = s_query.First();
                ViewBag.IsTeams = true;

                List<Team> teams = (from m in db.SeasonTeamPlayers
                                         where m.Season.SeasonId == season.SeasonId
                                         select m.Team).ToList().Distinct().ToList();

                foreach (var t in teams)
                {
                    TableTeamsHelper tth = new TableTeamsHelper();

                    tth.Team = t;
                    tth.TotalMatches = (from m in db.Matchs
                                        where (m.Team1.TeamId == t.TeamId ||
                                        m.Team2.TeamId == t.TeamId) && m.Season.SeasonId == season.SeasonId
                                        select m.MatchId).Count();

                    tth.WinsMatches = (from m in db.Matchs
                                       where ((m.Team1.TeamId == t.TeamId && m.FinalScore1 > m.FinalScore2) ||
                                       (m.Team2.TeamId == t.TeamId && m.FinalScore1 < m.FinalScore2)) && m.Season.SeasonId == season.SeasonId
                                       select m.MatchId).Count();

                    tth.LostMatches = tth.TotalMatches - tth.WinsMatches;

                    if (tth.LostMatches > 0)
                    {
                        tth.Relation = Math.Round((tth.WinsMatches / tth.LostMatches), 2);
                    }
                    else
                    {
                        tth.Relation = tth.WinsMatches;
                    }

                    tth.Balance = (tth.WinsMatches - tth.LostMatches);
                    tth.TotalPoints = (2 * tth.WinsMatches + 1 * tth.LostMatches);

                    TeamsList.Add(tth);
                }

                ViewBag.TeamsList = TeamsList;

            }
            return View();
        }

    }

    public class TableTeamsHelper
    {
        public Team Team        { get; set; }
        public int TotalMatches { get; set; }
        public double WinsMatches  { get; set; }
        public double LostMatches  { get; set; }
        public double TotalPoints  { get; set; }
        public double Balance      { get; set; }
        public double Relation   { get; set; }
    }

}